<?php

namespace App\Modules\Manage\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class CategoryModel extends Model
{

    protected $table = 'pig_category';
    protected $primaryKey = 'id';

    protected $fillable = [
        'id','title','is_show','sort','create_time','update_time'
    ];

    public $timestamps = false;

    static function cateInsert(array $param)
    {
        $result = self::create($param);
        return $result;
    }
//admin模块 分页
    static function cateList()
    {
        $lists = self::select(DB::raw('id,title,sort,is_show, from_unixtime(create_time) create_time, from_unixtime(update_time) update_time'))
            ->orderBy('is_show','desc')
            ->orderBy('sort','desc')
            ->paginate(2);
        return $lists;
    }
//admin模块 全部列表
    static function cateAll(){
        $lists = self::select(DB::raw('id,title,sort,is_show, from_unixtime(create_time) create_time, from_unixtime(update_time) update_time'))
            ->orderBy('is_show','desc')
            ->orderBy('sort','desc')
            ->get();
        return $lists;
    }
    //admin模块
    static function cateOne( $id)
    {
        $id = (int) $id;
        $result = self::where('id',$id)->first();
        if ($result) {
            $result = $result->toArray();
        }
        return $result;
    }
    //admin模块
    static function cateUpdate(array $param)
    {
        $id = (int) $param['id'];
        unset($param['id']);
        $result = self::where('id',$id)->update($param);
        return $result;
    }

    //home模块
    static function homeAllCates()
    {
        $results = self::where('is_show',1)->select('id','title')->get();
        return $results;
    }
}
